package com.chujian.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.chujian.DBUtil.DBUtil;
import com.chujian.dao.FoodDao;
import com.chujian.model.Food;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
/**
 * ��Ʒ��
 * @author cc
 *
 */
public class FoodDaoImpl implements FoodDao {

	@Override
	public boolean addNewFood(Food newFood) {
		// TODO Auto-generated method stub
		String sql=" insert into food(food_name,food_categary,food_price,food_url,food_unit,food_status,food_zan,food_cai,sell_count)values(?,?,?,?,?,?,?,?,?) ";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, newFood.getFoodName());
			ptmt.setString(2, newFood.getFoodCategary());
			ptmt.setFloat(3, newFood.getFoodPrice());
			ptmt.setString(4, newFood.getFoodUrl());
			ptmt.setString(5, newFood.getFoodUnit());
			ptmt.setString(6, newFood.getFoodStatus());
			ptmt.setInt(7, 0);
			ptmt.setInt(8, 0);
			ptmt.setInt(9, 0);
			int count=ptmt.executeUpdate();
			if(count>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		return false;
	}

	@Override
	public boolean updateFood(Food food) {
		// TODO Auto-generated method stub
		String sql="update food set food_name=?,food_price=?,food_unit=?,food_status=? where food_id=?";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, food.getFoodName());
			ptmt.setFloat(2, food.getFoodPrice());
			ptmt.setString(3, food.getFoodUnit());
			ptmt.setString(4, food.getFoodStatus());
			ptmt.setInt(5, food.getFoodId());
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}

	@Override
	public boolean deleteFood(String foodName) {
		
		String sql=" delete from food where food_name=? ";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, foodName);
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}

	@Override
	public List<Food> qeuryFoodList(String categary) {
		// TODO Auto-generated method stub
		
		String sql=" select * from food where food_categary=? ";
		Connection conn=DBUtil.getConnection();
		List<Food> list=new ArrayList<Food>();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, categary);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String foodName=rs.getString("food_name");
				String foodCategary=rs.getString("food_categary");
				float foodPrice=rs.getFloat("food_price");
				String foodUrl=rs.getString("food_url");
				String foodUnit=rs.getString("food_unit");
				String foodStatus=rs.getString("food_status");
				int foodZan=rs.getInt("food_zan");
				int foodCai=rs.getInt("food_cai");
				int sellCount=rs.getInt("sell_count");
				int foodId=rs.getInt("food_id");
				
				Food food=new Food();
				food.setFoodName(foodName);
				food.setFoodCategary(foodCategary);
				food.setFoodPrice(foodPrice);
				food.setFoodStatus(foodStatus);
				food.setFoodUnit(foodUnit);
				food.setFoodUrl(foodUrl);
				food.setFoodZan(foodZan);
				food.setFoodCai(foodCai);
				food.setSellCount(sellCount);
				food.setFoodId(foodId);
				list.add(food);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public Food qeuryFoodByName(String foodName) {
		String sql=" select * from food where food_name=? ";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1,foodName);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
//				String foodName=rs.getString("food_name");
				String foodCategary=rs.getString("food_categary");
				float foodPrice=rs.getFloat("food_price");
				String foodUrl=rs.getString("food_url");
				String foodUnit=rs.getString("food_unit");
				String foodStatus=rs.getString("food_status");
				int foodZan=rs.getInt("food_zan");
				int foodCai=rs.getInt("food_cai");
				int sellCount=rs.getInt("sell_count");
				int foodId=rs.getInt("food_id");
				
				Food food=new Food();
				food.setFoodName(foodName);
				food.setFoodCategary(foodCategary);
				food.setFoodPrice(foodPrice);
				food.setFoodStatus(foodStatus);
				food.setFoodUnit(foodUnit);
				food.setFoodUrl(foodUrl);
				food.setFoodZan(foodZan);
				food.setFoodCai(foodCai);
				food.setSellCount(sellCount);
				food.setFoodId(foodId);
				
				return food;
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		return null;
	}
	@Override
	public List<String> getAllCategary() {
		String sql=" select distinct food_categary from food ";
		Connection conn=DBUtil.getConnection();
		List<String> list=new ArrayList<String>();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String foodCategary=rs.getString("food_categary");
				if(!list.contains(foodCategary)){
					list.add(foodCategary);
				}
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		return list;
	}

	@Override
	public List<Food> qeuryAllFood() {
		String sql=" select * from food ";
		Connection conn=DBUtil.getConnection();
		List<Food> list=new ArrayList<Food>();
		int count=0;
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String foodName=rs.getString("food_name");
				String foodCategary=rs.getString("food_categary");
				float foodPrice=rs.getFloat("food_price");
				String foodUrl=rs.getString("food_url");
				String foodUnit=rs.getString("food_unit");
				String foodStatus=rs.getString("food_status");
				
				Food food=new Food();
				food.setFoodName(foodName);
				food.setFoodCategary(foodCategary);
				food.setFoodPrice(foodPrice);
				food.setFoodStatus(foodStatus);
				food.setFoodUnit(foodUnit);
				food.setFoodUrl(foodUrl);
				count++;
				list.add(food);
				System.out.println("FoodDaoImpl"+food.toString());
			}
			System.out.println(count+"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public boolean commentFood(String foodName, String zan_cai) {
		Food food=qeuryFoodByName(foodName);
		int amount=-1;
		if(zan_cai.equals("zan")){
			amount=food.getFoodZan();
			amount++;
		}else if (zan_cai.equals("cai")){
			amount=food.getFoodCai();
			amount++;
		}
		
		String sql_1="update food set food_zan=? where food_name=?";
		String sql_2="update food set food_cai=? where food_name=?";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=null;
			if(zan_cai.equals("zan")){
				ptmt=(PreparedStatement) conn.prepareStatement(sql_1);
			}else if (zan_cai.equals("cai")){
				ptmt=(PreparedStatement) conn.prepareStatement(sql_2);

			}
			ptmt.setInt(1, amount);
			ptmt.setString(2, foodName);
			
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}
}


